package com.qili.controller.adminManager.zx;

import com.qili.core.annotation.Log;
import com.qili.entity.base.BaseDict;
import com.qili.entity.base.BaseStageZx;
import com.qili.exception.MyException;
import com.qili.service.base.BaseDictService;
import com.qili.service.base.BaseStageZxService;
import com.qili.util.JsonUtil;
import com.qili.util.ReType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/adminManager/zx")
public class StageManagerZxController {

    @Autowired
    BaseStageZxService baseStageService;
    @Autowired
    BaseDictService baseDictService;
    @GetMapping(value = "stageManager")
    @RequiresPermissions("stage:show")
    public String showStage(Model model, HttpServletRequest request) {
        List<BaseDict> JDlist = baseDictService.selectByTypeCode("JD001ZX");
        List<BaseDict> PTYPEList = baseDictService.selectByTypeCode("PTYPE001ZX");
        model.addAttribute("JDlist", JDlist);
        model.addAttribute("PTYPEList", PTYPEList);
        return "/adminManager/zx/stageManagerZx/stage";
    }
    @GetMapping(value = "updateStage")
    public String UpdateStage(Model model,HttpServletRequest request,BaseStageZx baseStage) {
        List<BaseDict> JDlist = baseDictService.selectByTypeCode("JD001ZX");
        List<BaseDict> PTYPEList = baseDictService.selectByTypeCode("PTYPE001ZX");
        BaseStageZx stage = baseStageService.selectByPrimaryKey(baseStage.getId());
        model.addAttribute("stage",stage);
        model.addAttribute("JDlist", JDlist);
        model.addAttribute("PTYPEList", PTYPEList);
        return "/adminManager/zx/stageManagerZx/update_stage";
    }

    @GetMapping(value = "addStage")
    public String addStage(Model model,HttpServletRequest request) {
        List<BaseDict> JDlist = baseDictService.selectByTypeCode("JD001ZX");
        List<BaseDict> PTYPEList = baseDictService.selectByTypeCode("PTYPE001ZX");
        model.addAttribute("JDlist", JDlist);
        model.addAttribute("PTYPEList", PTYPEList);
        return "/adminManager/zx/stageManagerZx/add_stage";
    }

//    public ReType selectByProjectTypeNo(BaseStageZx baseStage, Model model, String page, String limit) {
//        ReType AlLevel= new ReType();
//        try {
//            //分页
//            com.github.pagehelper.Page<BaseStageZx> tPage = PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
//            List<BaseStageZx> AlstData = baseStageService.selectByProjectTypeNo(baseStage);
//            AlLevel.setCount(tPage.getTotal());
//            AlLevel.setData(AlstData);
//            AlLevel.setCode(0);
//        } catch (NumberFormatException e) {
//            AlLevel.setMsg("查询异常！！"+e.getMessage());
//            AlLevel.setCode(1);
//            e.printStackTrace();
//        }
//        return AlLevel;
//    }

    @ApiOperation(value = "/stageList", httpMethod = "GET", notes = "项目类型编号")
    @GetMapping(value = "stageList")
    @ResponseBody
    public ReType selectByProjectTypeNo(BaseStageZx baseStage, Model model, String page, String limit) {
        ReType re = baseStageService.show(baseStage, Integer.valueOf(page), Integer.valueOf(limit));
        return re;
    }
    @ApiOperation(value = "/delStage", httpMethod = "POST", notes = "删除")
    @PostMapping(value = "delStage")
    @ResponseBody
    public JsonUtil delStage(String id) {
        JsonUtil json  = new JsonUtil();
        try {
            int Aires = baseStageService.delStage(id);
            if (Aires > 0) {
                json.setFlag(true);
            } else {
                json.setFlag(false);
                json.setMsg("数据库返回更新失败!!!");
            }
        }catch (Exception e){
            json.setFlag(false);
            json.setMsg("执行异常!!!"+e.getMessage());
        }
        return json;
    }

    @ApiOperation(value = "/updateStage", httpMethod = "POST", notes = "更新项目阶段")
    @PostMapping(value = "updateStage")
    @ResponseBody
    public JsonUtil updateStage(BaseStageZx baseStage) {
        JsonUtil json = new JsonUtil();
        json.setFlag(false);
        if (baseStage == null) {
            json.setFlag(false);
            json.setMsg("获取数据失败!!!");
            return json;
        }
        try {
            baseStageService.updateByPrimaryKey(baseStage);
        } catch (MyException e) {
            e.printStackTrace();
            json.setFlag(false);
            json.setMsg("操作失败!!!"+e.getMessage());
            return json;
        }
        json.setFlag(true);
        json.setMsg("操作成功");
        return json;
    }
    @ApiOperation(value = "/add", httpMethod = "POST", notes = "增加项目阶段")
    @Log(desc = "增加项目阶段", type = Log.LOG_TYPE.UPDATE)
    @PostMapping(value = "add")
    @ResponseBody
    public JsonUtil addStage(BaseStageZx baseStage) {
        JsonUtil json = new JsonUtil();
        json.setFlag(false);
        if (baseStage == null) {
            json.setFlag(false);
            json.setMsg("获取数据失败!!!");
            return json;
        }
        if (baseStage.getStageNo().equals("001")){
            baseStage.setOrderNum(1);
        }else if (baseStage.getStageNo().equals("002")){
            baseStage.setOrderNum(2);
        }else if (baseStage.getStageNo().equals("003")){
            baseStage.setOrderNum(3);
        }else if (baseStage.getStageNo().equals("004")){
            baseStage.setOrderNum(4);
        }else if (baseStage.getStageNo().equals("005")){
            baseStage.setOrderNum(5);
        }
        try {
            baseStageService.insert(baseStage);
        } catch (MyException e) {
            e.printStackTrace();
            json.setFlag(false);
            json.setMsg("添加失败!!!"+e.getMessage());
            return json;
        }
        json.setFlag(true);
        json.setMsg("添加成功");
        return json;
    }
}
